/**
 * GeoSOA/Spatialytics/mapstyles/contexts/DynamicProportionalSymbolsThematicContext.js
 *
 * DynamicProportionalSymbolsThematicContext
 *
 * Copyright (C) 2008-2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090918
 **/

dojo.provide("GeoSOA.Spatialytics.mapstyles.contexts.DynamicProportionalSymbolsThematicContext");

dojo.require("GeoSOA.Spatialytics.mapstyles.contexts.BaseThematicContext");
dojo.require("GeoSOA.Spatialytics.mapstyles.contexts.DynamicThematicContext");
dojo.require("GeoSOA.util.HTMLUtils");

/**
 * Context object for dynamic proportional symbols
 * Inherits from BaseThematicContext and DynamicThematicContext
 * @param {Object} options Options passed to the constructor
 * 
 * @constructor
 */
dojo.declare("GeoSOA.Spatialytics.mapstyles.contexts.DynamicProportionalSymbolsThematicContext",
		[GeoSOA.Spatialytics.mapstyles.contexts.BaseThematicContext,
		 GeoSOA.Spatialytics.mapstyles.contexts.DynamicThematicContext], {
	
	constructor: function (options) {

		// current context for closures
		var that = this;
	
		var options = options || {};
		
		this.minSize = options.minSize || 1;
		this.maxSize = options.maxSize || 10;
		
		this.minValue = 0;
		this.maxValue = 0;

		/**
		 * Closure function to lookup the feature's value and scale it
		 * according to the difference between min and max in data set and
		 * the specified size range for symbols
		 * 
		 * @param {OpenLayers.Feature} feature OpenLayers feature object to
		 * apply a style to (passed internally by OpenLayers)
		 * @return {Number} Size
		 * @public
		 */
		this.getSize = function (feature) {
			var val = feature.attributes[that.valueAttribute];
			// radius directly proportional to value (scales linearly)
			//return that.minSize + val * (that.maxSize - that.minSize) / (that.maxValue - that.minValue); 
			return 2;
		};
		
		this.getColor = function (feature) {
			// TODO: this should not be here
			return "#ffcc66";
		};
		
		/**
		 * Observer function (closure with the current object) to be
		 * added when calling SOLAPContext.addAfterLayerRefreshObserver().
		 * 
		 * @return {Function} callback closure function
		 * @private
		 */

		this._minmaxObserverClosure = function (solapContext) {
			// set up an observer for dynamic min/max bounds
			
			var minmax = that.calcMinMaxValues(
					solapContext.getVectors(), this.valueAttribute);
			that.minValue = minmax.min;
			that.maxValue = minmax.max;
		};
		this.solapContext.afterLayerRefreshObservable.register(this._minmaxObserverClosure);
	},

	/**
	 * Disposes of this object (unregisters itself from the solapContext
	 */
	dispose: function () {
		this.solapContext.afterLayerRefreshObservable.unregister(this._minmaxObserverClosure);
	}
	
});
